package org.inspiration.dudu.content.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.inspiration.dudu.content.model.dto.CommentDTO;
import org.inspiration.dudu.content.model.entity.CommentEntity;

import java.util.List;

/**
 * 评论数据访问层接口
 * 继承自BaseMapper，提供基本的CRUD操作
 */
@Mapper
public interface CommentMapper extends BaseMapper<CommentEntity> {
    
    /**
     * 创建新的评论
     * 
     * @param commentEntity 评论实体
     */
    @Insert("INSERT INTO comment (id, content_file_path, user_id, parent_id, replied_username, content_type, replied_user_id) " +
            "VALUES (#{id}, #{contentFilePath}, #{userId}, #{parentId}, #{repliedUsername}, #{contentType}, #{repliedUserId})")
    void createComment(CommentEntity commentEntity);

    /**
     * 根据父评论ID升序查找评论
     * 
     * @param parentId 父评论ID
     * @param offset 偏移量
     * @param pageSize 页面大小
     * @return 评论列表
     */
    @Select("select * from comment where parent_id = #{parentId} order by created_at asc limit #{offset}, #{pageSize}")
    List<CommentEntity> getCommentsByParentIdOrderByASC(String parentId, Integer offset, Integer pageSize);
    
    /**
     * 根据父评论ID降序查找评论
     * 
     * @param parentId 父评论ID
     * @param offset 偏移量
     * @param pageSize 页面大小
     * @return 评论列表
     */
    @Select("select * from comment where parent_id = #{parentId} order by created_at desc limit #{offset}, #{pageSize}")
    List<CommentEntity> getCommentsByParentIdOrderByDESC(String parentId, Integer offset, Integer pageSize);
    
    /**
     * 增加评论的点赞数
     * 
     * @param contentId 评论ID
     */
    @Update("UPDATE comment set like_count = like_count + 1 where id = #{contentId}")
    void increaseLikeCount(String contentId);

    /**
     * 减少评论的点赞数
     * 
     * @param contentId 评论ID
     */
    @Update("UPDATE comment set like_count = like_count - 1 where id = #{contentId}")
    void decreaseLikeCount(String contentId);

    /**
     * 根据评论ID获取用户ID
     * 
     * @param contentId 评论ID
     * @return 用户ID
     */
    @Select("select user_id from comment where id = #{contentId}")
    Long getUserIdByContentId(String contentId);

    /**
     * 增加评论的评论数
     * 
     * @param contentId 评论ID
     */
    @Update("update comment set comment_count = comment_count + 1 where id = #{contentId}")
    void increaseCommentCount(String contentId);

    /**
     * 根据评论ID获取评论实体
     * 
     * @param contentId 评论ID
     * @return 评论实体
     */
    @Select("select * from comment where id = #{contentId}")
    CommentEntity getCommentByContentId(String contentId);


    /**
     * 将评论标记为已读
     */
    @Update("update comment set is_read = 1 where user_id = #{userId} and parent_id = #{parentId} and content_type = #{contentType} and replied_user_id = #{repliedUserId}")
    void markAsRead(CommentDTO commentDTO);

    @Select("select * from comment where replied_user_id = #{userId} order by created_at desc limit #{offset}, #{pageSize}")
    List<CommentEntity> getReceiveCommentsByUserIdOrderByDESC(Long userId, Integer offset, Integer pageSize);
}
